<?php
$PurviewLevel=0;
require("../../inc/common.inc.php");
require_once(dirname(__FILE__)."/../"."Admin_ChkPurview.php");
$appinfo = $db->getinfo("select * from mx_tuisong_app where tid='$id'");
$tsucaiid = $appinfo["tsucaiid"];
if($appinfo["tuserids"]!=""){
	$appuserstr = substr($appinfo["tuserids"],0,strlen($appinfo["tuserids"])-1);
}else{
$wherestr="isok=1";
if($appinfo["tprovince"]!=""){
$wherestr .= "and province like '%".$appinfo["tprovince"]."%'";
}
if($appinfo["tcity"]!=""){
$wherestr .= "and city like '%".$appinfo["tcity"]."%'";
}
if($appinfo["tarea"]!=""){
$wherestr .= "and district like '%".$appinfo["tarea"]."%'";
}
if($appinfo["tgroupid"]!=0){
	$wherestr.=" and groupid='".$appinfo["tgroupid"]."'";
}
if($appinfo["tsex"]!=2){
	$wherestr.=" and sex='".$appinfo["tsex"]."'";
}
$userquery = $db->excu("select * from mx_app_user where ".$wherestr);
$userinfo = array();
while($userarr = $db->fetch_array($userquery)){
	$userinfo[] = $userarr;
}
if(count($userinfo)==0){
	$db->excu("delete from `mx_tuisong_app` where `tid`='$tid'");
	$fun->popmassage("您设置的条件未找到符合要求的用户","","popback");
	exit();
}else{
	$appuserstr="";
	foreach($userinfo as $key=>$value){
		if($key==0){
			$appuserstr.=$value["id"];
		}else{
			$appuserstr.=",".$value["id"];
		}
	}
}

}

if($ttype!=2){
$sucaiinfo = $db->getinfo("select * from mx_sucai_app where aid ='$tsucaiid' limit 1");
if($ttype==1){
	$msg_content="url-:".$webdb["weburl"]."/".apppushurl."?id=".$tsucaiid;
}elseif($ttype==2){
	$msg_content="txt-:".$appinfo["ttitle"]."：".$sucaiinfo["acontent"];	
}elseif($ttype==3){
	$msg_content="url-:".$sucaiinfo["aurl"];
}
}else{
	$msg_content="txt-:".$appinfo["ttitle"]."：".$appinfo["tcontent"];	
}



$msg_content1 = array("n_content"=>$msg_content,"n_extras"=>array("ios"=>array("badge"=>1)));
$msg_content1 = json_encode($msg_content1);
$platform1 = "ios";

$msg_content2 = array("message"=>$msg_content);
$msg_content2 = json_encode($msg_content2);
$platform2 = "android";


$obj = new jpush(masterSecret,appkeys);			 
$res1 = $obj->send($tid, 3, $appuserstr, 1, $msg_content1, $platform1);
$res2 = $obj->send($tid, 3, $appuserstr, 2, $msg_content2, $platform2);	
if($res2["errcode"]==0&&$res2["errcode"]==0){
	$db->excu("update `mx_tuisong_app` set isok=1 where tid='$id'");
	echo "已发送";
}else{
	echo '<a href="javascript:void(0);" onclick="apprepost('.$id.')">失败重发</a>';
}
?>